#!/bin/bash

# ================================
# CentOS 7 更换为阿里云 YUM 源脚本
# Author: AI助手
# ================================

echo "🔄 正在备份原有 repo 文件..."

cd /etc/yum.repos.d/
sudo mkdir -p backup
sudo mv *.repo backup/

echo "📥 下载阿里云 CentOS 7 repo 文件..."

sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 判断是否成功下载
if [ $? -eq 0 ]; then
  echo "✅ 成功更换为阿里云 YUM 源"
else
  echo "❌ 下载 repo 文件失败，请检查网络连接"
  exit 1
fi

echo "🧹 清除 YUM 缓存..."
sudo yum clean all

echo "🔥 生成新的 YUM 缓存..."
sudo yum makecache

echo "📦 是否安装常用工具（vim wget net-tools git）？(y/n)"
read INSTALL_TOOLS

if [[ "$INSTALL_TOOLS" == "y" || "$INSTALL_TOOLS" == "Y" ]]; then
  echo "🛠 安装常用工具中..."
  sudo yum install -y vim wget net-tools git curl
  if [ $? -eq 0 ]; then
    echo "✅ 工具安装完成"
  else
    echo "⚠️ 工具安装失败，可能不影响系统"
  fi
fi

echo "🎉 操作已完成，YUM 源已更换为阿里云镜像。"
